package com.allwees.bs.c.module.user.dto;

import com.allwees.bs.c.module.base.dto.BaseDto;
import com.allwees.bs.c.module.user.entity.UserEntity;
import com.allwees.bs.core.model.user.constant.EUserSourceType;
import com.allwees.core.common.util.reflect.PojoUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
//@ApiModel
public class UserDto extends BaseDto<UserDto> {
	public final static String[] ORDER_REVIEW_FIELDS = {"id", "firstName", "lastName", "avatar"};

	private String uuid;

	private String email;

	private String firstName;

	private String lastName;

	private String password;

	private String inviterUuid;

	private String shareCode;

	private EUserSourceType sourceType;

	private String birthday;

	private String avatar;

	private String itu;

	private String phone;

	private String language;

	private Integer country;

	private Integer city;

	private String address;

	private String zipCode;

	private String currency;

	private Integer gender;

	private String aboutMe;

	private Integer identityVerify;

	private Integer emailVerified;

	private Integer phoneVerified;

	private Date latestLoginAt;

	private Date createdAt;

	private Date updatedAt;

	private String cartUuid;

	private int cartCount = 0;

	private String deviceSource;

	private String deviceId;

	private String deviceModel;

	private String systemVersion;

	private String market;

	private String bundleId;

	private String appName;

	private String appVersion;

	private Float longitude;

	private Float latitude;

	private BigDecimal points;

	private BigDecimal cashs;

	private boolean third;

	private boolean hasOrder;

	private String promoCode;

	public UserDto(UserEntity user) {
		this();
		PojoUtil.copyWithOmit(this, user, "password");
		if(Objects.isNull(user.getBirthdayYear())) {
			this.birthday = null;
		} else {
			StringBuilder sb = new StringBuilder();
			sb.append(autoGenericCode(user.getBirthdayYear(), 4)).append("-")
					.append(autoGenericCode(user.getBirthdayMonth(), 2)).append("-")
					.append(autoGenericCode(user.getBirthdayDay(), 2));
			this.birthday = sb.toString();
		}
		this.setThird(StringUtils.isNotBlank(user.thirdId()));
	}

	/**
	 * 不够位数的在前面补0，保留num的长度位数字
	 *
	 * @param code
	 * @return
	 */
	private String autoGenericCode(int code, int num) {
		return String.format("%0" + num + "d", code + 1);
	}
}
